package com.hsck.order.kds.main.dialog.site

import android.app.Activity
import android.app.Dialog
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.hjq.toast.Toaster
import com.hsck.order.kds.R
import com.hsck.order.kds.base.utils.setOnSingleClickListener
import com.hsck.order.kds.base.isnoam.view.SpacesItemDecoration
import com.hsck.order.kds.databinding.SelectSiteDialogLayoutBinding
import com.hsck.order.kds.main.fragment.standard.bean.TestListBean

/**
 * 选择站点
 */
class SelectSiteDialog(context: Activity) : Dialog(context, R.style.Dialog_FullScreen) {
    private lateinit var onCliclLisener: SelectSiteListener
    private val activity: Activity = context

    private var binding: SelectSiteDialogLayoutBinding = DataBindingUtil.inflate(
        LayoutInflater.from(context),
        R.layout.select_site_dialog_layout,
        null,
        false
    )

    private var selectSiteDialogAdapter: SelectSiteDialogAdapter? = null
    private var itemBean: TestListBean? = null

    init {
        createDialog()
    }

    private fun createDialog() {
        setContentView(binding.root)
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        val lp = window?.attributes
        lp?.width = WindowManager.LayoutParams.MATCH_PARENT
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT

        lp?.alpha = 1f
        window?.attributes = lp
        if (activity.isFinishing) {
            return
        }
        initRecyclerView()
        binding.tvSelectSite.setOnSingleClickListener {
            if (selectSiteDialogAdapter!!.itemCount > 0) {
                if (itemBean == null) {
                    Toaster.show("请选择当前设备绑定的站点")
                    return@setOnSingleClickListener
                }
            }
            dismiss()
            onCliclLisener.onSelectSiteListener(itemBean)
        }
    }

    private fun initRecyclerView() {
        selectSiteDialogAdapter = SelectSiteDialogAdapter(activity)
        val staggeredGridLayoutManager =
            StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
        staggeredGridLayoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
        staggeredGridLayoutManager.isItemPrefetchEnabled = true
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.setItemViewCacheSize(20)
        binding.recyclerView.layoutManager = staggeredGridLayoutManager
        binding.recyclerView.adapter = selectSiteDialogAdapter
        val spacingInPixels = activity.resources.getDimensionPixelSize(R.dimen.select_site_spacing)
        binding.recyclerView.addItemDecoration(SpacesItemDecoration(spacingInPixels))
        selectSiteDialogAdapter!!.setSiteSelectedListener(object :
            SelectSiteDialogAdapter.SiteSelectedListener {
            override fun onSiteSelected(itemBean: TestListBean, position: Int) {
                this@SelectSiteDialog.itemBean = itemBean
            }
        })
    }

    fun showSelectSite(list: MutableList<TestListBean>) {
        binding.siteSize = list.size
        if (list.isNotEmpty()) {
            selectSiteDialogAdapter?.setListData(list)
        } else {
            this.itemBean = null
        }
        show()
    }

    fun setSelectQuantityListener(clickListener: SelectSiteListener) {
        this.onCliclLisener = clickListener
    }

    interface SelectSiteListener {
        fun onSelectSiteListener(itemBean: TestListBean?)
    }
}
